package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketAdminAdminService;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

public class MarketAdminAdminServiceImpl implements MarketAdminAdminService {
    @Override
    public List<MarketAdmin> getList(int page, int limit, String sort, String order, String username) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%"+username+"%");
        }
        marketAdminExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketAdmin> marketAdminList = mapper.selectByExample(marketAdminExample);
        session.commit();
        session.close();
        return marketAdminList;
    }

    @Override
    public MarketAdmin create(String password, String username, Integer[] roleIds) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername(username);
        marketAdmin.setPassword(password);
        marketAdmin.setRoleIds(roleIds);
        marketAdmin.setAddTime(LocalDateTime.now());
        marketAdmin.setUpdateTime(LocalDateTime.now());
        mapper.insert(marketAdmin);
        session.commit();
        session.close();
        return marketAdmin;
    }

    @Override
    public MarketAdmin update(String avatar, Integer[] roleIds, String id, String password, String username) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        MarketAdmin marketAdmin = mapper.selectByPrimaryKey(Integer.valueOf(id));
        marketAdmin.setAvatar(avatar);
        marketAdmin.setRoleIds(roleIds);
        marketAdmin.setUpdateTime(LocalDateTime.now());
        marketAdmin.setUsername(username);
        marketAdmin.setPassword(password);
        mapper.updateByPrimaryKey(marketAdmin);
        session.commit();
        session.close();
        return marketAdmin;
    }

    @Override
    public void delete(String id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAdminMapper mapper = session.getMapper(MarketAdminMapper.class);
        mapper.deleteByPrimaryKey(Integer.valueOf(id));
        session.commit();
        session.close();
    }
}
